package framework.ui;

import org.openems.entity.BooleanEntity;
import org.openems.entity.Entity;
import org.openems.entity.Key;

import framework.DomainService;
import framework.EntityStoreListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.Pane;

public class EmsCheckBox extends Pane {
	private CheckBox checkbox = new CheckBox();
	
	public EmsCheckBox(final Key key, final DomainService service) {
		this.getChildren().add(checkbox);
		checkbox.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent arg0) {
				BooleanEntity e = (BooleanEntity)service.getEntity(key);
				e.setValue(checkbox.isSelected());
				service.putEntity(e);
			}
		});
		service.addEntityStoreListener(new EntityStoreListener() {
			@Override
			public void valueChanged(Entity entity) {
				updateUi((BooleanEntity)entity);
			}
		}, key);
		updateUi((BooleanEntity)service.getEntity(key));
	}

	protected void updateUi(BooleanEntity entity) {
		checkbox.setText(entity.getName());
		checkbox.setSelected(entity.getValue());
	}


}
